package com.ming.sportdemo.Aop;

import com.ming.sportdemo.Exception.RoleException;
import com.ming.sportdemo.annotation.NeedRole;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.net.URLDecoder;

@Aspect
@Component
public class AuthorizeAspect {
    @Pointcut("execution (public * com.ming.sportdemo.controller.UtilController.get*(..))")
    public void verify(){

    }
    @Before("verify()")
    public void doVerify() throws UnsupportedEncodingException, RoleException {
        //System.out.print("doVerify");
        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attr.getRequest();
        String role = URLDecoder.decode(request.getHeader("role"),"UTF-8");//从前端请求头获取role
        System.out.println("role:"+role);
        if (role.equals("管理员")||role.equals("超级管理员")){
            System.out.println("身份验证成功，可以继续操作！");
        }else {
            throw new RoleException("身份验证失败，角色权限异常!");
        }

    }


}
